/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include <sstream>
#include "../../common/data_utils.h"
#include "kernel_tiling/kernel_tiling.h"
#include "tiling/platform/platform_ascendc.h"
#include "op_host/matmul_channelsplit_custom_tiling.h"

#ifndef ASCENDC_CPU_DEBUG
#include "acl/acl.h"
extern void matmul_channelsplit_custom_do(uint32_t coreDim, void* stream,
    uint8_t* a, uint8_t* b, uint8_t* bias, uint8_t* c,
    uint8_t* workspace, uint8_t* tiling);
#else
#include "tikicpulib.h"
extern "C" void matmul_channelsplit_custom(uint8_t* a, uint8_t* b, uint8_t* bias, uint8_t* c,
    uint8_t* workspace, uint8_t* tiling);
#endif

namespace {
constexpr bool IS_BIAS = true;
constexpr bool IS_A_TRANS = false;
constexpr bool IS_B_TRANS = false;
constexpr int32_t BLOCK_SIZE = 16;

struct MatrixFileSize
{
    size_t x1FileSize;
    size_t x2FileSize;
    size_t yFileSize;
    size_t biasFileSize;
};
}

namespace MatmulHost {
static size_t GetSysWorkSpaceSize()
{
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    return static_cast<size_t>(ascendcPlatform->GetLibApiWorkSpaceSize());
}

// CPU debug mode
#ifdef ASCENDC_CPU_DEBUG
void TestMatmulCpu(int64_t m, int64_t n, int64_t k, const MatrixFileSize& matrixFileSize)
{
    size_t x1FileSize = matrixFileSize.x1FileSize;
    size_t x2FileSize = matrixFileSize.x2FileSize;
    size_t yFileSize = matrixFileSize.yFileSize;
    size_t biasFileSize = matrixFileSize.biasFileSize;
    // Query workspace size
    size_t workspaceSize = GetSysWorkSpaceSize();
    uint8_t* x1 = (uint8_t*)AscendC::GmAlloc(x1FileSize);
    uint8_t* x2 = (uint8_t*)AscendC::GmAlloc(x2FileSize);
    uint8_t* bias = nullptr;
    uint8_t* y = (uint8_t*)AscendC::GmAlloc(yFileSize);
    uint8_t* workspace = (uint8_t*)AscendC::GmAlloc(workspaceSize);
    ReadFile("../input/x1_gm.bin", x1FileSize, x1, x1FileSize);
    ReadFile("../input/x2_gm.bin", x2FileSize, x2, x2FileSize);
    if (IS_BIAS) {
        bias = (uint8_t*)AscendC::GmAlloc(biasFileSize);
        ReadFile("../input/bias_gm.bin", biasFileSize, bias, biasFileSize);
    }
    size_t tilingFileSize = sizeof(TCubeTiling);
    uint8_t* tiling = (uint8_t*)AscendC::GmAlloc(tilingFileSize);
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    MatmulHost::MatmulCaseParams testCaseParams{static_cast<int32_t>(ascendcPlatform->GetCoreNumAic()),
        static_cast<int32_t>(m), static_cast<int32_t>(n), static_cast<int32_t>(k), IS_BIAS, IS_A_TRANS, IS_B_TRANS};
    // Calculate Tiling
    const auto& tilingData = MatmulHost::GenerateTiling(testCaseParams);
    memcpy_s(tiling, tilingFileSize, &tilingData, tilingFileSize);
    ICPU_RUN_KF(matmul_channelsplit_custom, tilingData.usedCoreNum, x1, x2, bias, y, workspace, tiling);
    WriteFile("../output/output.bin", y, yFileSize);
    AscendC::GmFree((void*)x1);
    AscendC::GmFree((void*)x2);
    AscendC::GmFree((void*)y);
    AscendC::GmFree((void*)workspace);
    AscendC::GmFree((void*)tiling);
    if (IS_BIAS) {
        AscendC::GmFree((void*)bias);
    }
}
// NPU
#else
void MatmulOp(uint8_t* x1, uint8_t* x2, uint8_t* y, uint8_t* bias, int64_t m, int64_t n, int64_t k,
    void* stream = nullptr)
{
    // Init args
    uint8_t* workspaceDevice = nullptr;

    // Query workspace size
    size_t workspaceSize = GetSysWorkSpaceSize();

    // Allocate workspace on device
    CHECK_ACL(aclrtMalloc((void**)&workspaceDevice, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST));

    // Initialize kernel with arguments and workspace pointer
    uint8_t* tilingHost = nullptr;
    uint8_t* tilingDevice = nullptr;
    size_t tilingFileSize = sizeof(TCubeTiling);
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    MatmulHost::MatmulCaseParams testCaseParams{static_cast<int32_t>(ascendcPlatform->GetCoreNumAic()),
        static_cast<int32_t>(m), static_cast<int32_t>(n), static_cast<int32_t>(k), IS_BIAS, IS_A_TRANS, IS_B_TRANS};
    // Calculate Tiling
    const auto tilingData = MatmulHost::GenerateTiling(testCaseParams);
    CHECK_ACL(aclrtMallocHost((void**)(&tilingHost), tilingFileSize));
    CHECK_ACL(aclrtMalloc((void**)&tilingDevice, tilingFileSize,
                          ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMemcpy(tilingHost, tilingFileSize, &tilingData,
                          tilingFileSize, ACL_MEMCPY_HOST_TO_HOST));
    CHECK_ACL(aclrtMemcpy(tilingDevice, tilingFileSize, tilingHost,
                          tilingFileSize, ACL_MEMCPY_HOST_TO_DEVICE));
    // Launch kernel
    matmul_channelsplit_custom_do(tilingData.usedCoreNum, stream, x1, x2, bias, y, workspaceDevice, tilingDevice);
    CHECK_ACL(aclrtFreeHost(tilingHost));
    CHECK_ACL(aclrtFree(workspaceDevice));
    CHECK_ACL(aclrtFree(tilingDevice));
}

void TestAclInit(aclrtContext& context, aclrtStream& stream, int64_t& deviceId)
{
    CHECK_ACL(aclInit(nullptr));
    CHECK_ACL(aclrtSetDevice(deviceId));
    CHECK_ACL(aclrtCreateContext(&context, deviceId));
    CHECK_ACL(aclrtCreateStream(&stream));
}

void TestAclDeInit(aclrtContext& context, aclrtStream& stream, int64_t& deviceId)
{
    CHECK_ACL(aclrtDestroyStream(stream));
    CHECK_ACL(aclrtDestroyContext(context));
    CHECK_ACL(aclrtResetDevice(deviceId));
    CHECK_ACL(aclFinalize());
}

void TestMatmul(int64_t m, int64_t n, int64_t k, const MatrixFileSize& matrixFileSize)
{
    size_t x1FileSize = matrixFileSize.x1FileSize;
    size_t x2FileSize = matrixFileSize.x2FileSize;
    size_t yFileSize = matrixFileSize.yFileSize;
    size_t biasFileSize = matrixFileSize.biasFileSize;

    aclrtContext context;
    aclrtStream stream = nullptr;
    int64_t deviceId = 0;
    TestAclInit(context, stream, deviceId);

    uint8_t* x1Host = nullptr;
    uint8_t* x1Device = nullptr;
    CHECK_ACL(aclrtMallocHost((void**)(&x1Host), x1FileSize));
    CHECK_ACL(aclrtMalloc((void**)&x1Device, x1FileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/x1_gm.bin", x1FileSize, x1Host, x1FileSize);
    CHECK_ACL(aclrtMemcpy(x1Device, x1FileSize, x1Host, x1FileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* x2Host = nullptr;
    uint8_t* x2Device = nullptr;
    CHECK_ACL(aclrtMallocHost((void**)(&x2Host), x2FileSize));
    CHECK_ACL(aclrtMalloc((void**)&x2Device, x2FileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/x2_gm.bin", x2FileSize, x2Host, x2FileSize);
    CHECK_ACL(aclrtMemcpy(x2Device, x2FileSize, x2Host, x2FileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* biasHost = nullptr;
    uint8_t* biasDevice = nullptr;
    if (IS_BIAS) {
        CHECK_ACL(aclrtMallocHost((void**)(&biasHost), biasFileSize));
        CHECK_ACL(aclrtMalloc((void**)&biasDevice, biasFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
        ReadFile("../input/bias_gm.bin", biasFileSize, biasHost, biasFileSize);
        CHECK_ACL(aclrtMemcpy(biasDevice, biasFileSize, biasHost, biasFileSize, ACL_MEMCPY_HOST_TO_DEVICE));
    }
    uint8_t* yHost = nullptr;
    uint8_t* yDevice = nullptr;
    CHECK_ACL(aclrtMallocHost((void**)(&yHost), yFileSize));
    CHECK_ACL(aclrtMalloc((void**)&yDevice, yFileSize, ACL_MEM_MALLOC_HUGE_FIRST));

    MatmulOp(x1Device, x2Device, yDevice, biasDevice, m, n, k, stream);
    CHECK_ACL(aclrtSynchronizeStream(stream));

    CHECK_ACL(aclrtMemcpy(yHost, yFileSize, yDevice, yFileSize, ACL_MEMCPY_DEVICE_TO_HOST));
    WriteFile("../output/output.bin", yHost, yFileSize);

    if (IS_BIAS) {
        CHECK_ACL(aclrtFree(biasDevice));
        CHECK_ACL(aclrtFreeHost(biasHost));
    }
    CHECK_ACL(aclrtFree(x1Device));
    CHECK_ACL(aclrtFreeHost(x1Host));
    CHECK_ACL(aclrtFree(x2Device));
    CHECK_ACL(aclrtFreeHost(x2Host));
    CHECK_ACL(aclrtFree(yDevice));
    CHECK_ACL(aclrtFreeHost(yHost));
    TestAclDeInit(context, stream, deviceId);
}
#endif
} // namespace MatmulHost

int32_t main(int32_t argc, const char* args[])
{
    int64_t inputParams[3] = {1, 1, 1};
    for (int32_t i = 1; i < argc && i < 4; ++i) { // 4 used for inputParams loop
        std::stringstream ss(args[i]);
        ss >> inputParams[i - 1];
    }
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance(SOC_VERSION);
    int64_t M = inputParams[0];
    int64_t N = inputParams[1];
    int64_t K = inputParams[2];
    MatrixFileSize matrixFileSize;
    // uint16_t represent half
    matrixFileSize.x1FileSize = static_cast<size_t>(M * K) * sizeof(uint16_t);
    matrixFileSize.x2FileSize = static_cast<size_t>(K * N) * sizeof(uint16_t);
    // float NZ cMatrix output, 16*8 fractal
    int32_t alignM = (static_cast<int32_t>(M) + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
    int32_t alignN = (static_cast<int32_t>(N) + BLOCK_SIZE / 2  - 1) / (BLOCK_SIZE / 2) * (BLOCK_SIZE / 2);
    matrixFileSize.yFileSize = static_cast<size_t>(alignM * alignN) * sizeof(float);
    matrixFileSize.biasFileSize = static_cast<size_t>(1 * N) * sizeof(float);
#ifdef ASCENDC_CPU_DEBUG
    MatmulHost::TestMatmulCpu(M, N, K, matrixFileSize);
#else
    MatmulHost::TestMatmul(M, N, K, matrixFileSize);
#endif
    return 0;
}
